package com.example.pingze.service;

import com.example.pingze.entity.PoemUserCollect;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.repository.PoemUserCollectRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class PoemUserCollectService {
    @Autowired
    private PoemUserCollectRepository repository;

    public PoemUserCollect collect(PoemUserCollect collect) {
        return repository.save(collect);
    }

    public List<PoemUserCollect> findPoemUserCollect(PoemUserCollect collect) {
        Specification specification = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(!StringUtils.isEmpty(collect.getAccount())){
                    predicates.add(criteriaBuilder.equal(root.get("account"),  collect.getAccount()));
                }
                if(!StringUtils.isEmpty(collect.getPoemId())){
                    predicates.add(criteriaBuilder.equal(root.get("poemId"), collect.getPoemId()));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return repository.findAll(specification);
    }

    public void delete(PoemUserCollect collect) {
        repository.delete(collect);
    }

    public List<PoemUserCollect> findPoemUserCollectByAccount(String account) {
        return repository.findPoemUserCollectByAccount(account);
    }

    public PoemUserCollect findPoemUserCollectByAccountAndPoemId(String account, String poemId) {
        return repository.findPoemUserCollectByAccountAndPoemId(account, poemId);
    }

    public Page getList(PoemUserCollect poemUserCollect, Pageable pageable) {
        Specification<PoemUserCollect> specification=new Specification<PoemUserCollect>() {
            @Override
            public Predicate toPredicate(Root<PoemUserCollect> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(!StringUtils.isEmpty(poemUserCollect.getId())){
                    predicates.add(criteriaBuilder.equal(root.get("id"),poemUserCollect.getId()));
                }
                if(!StringUtils.isEmpty(poemUserCollect.getAccount())){
                    predicates.add(criteriaBuilder.equal(root.get("account"),poemUserCollect.getAccount()));
                }
                if(!StringUtils.isEmpty(poemUserCollect.getPoemId())){
                    predicates.add(criteriaBuilder.equal(root.get("poemId"),poemUserCollect.getPoemId()));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Page page = repository.findAll(specification,pageable);
        return page;
    }
    public PoemUserCollect add(String account, String poemId){
        if(StringUtils.isEmpty(account)){
            throw new PingZeException("账号不能为空");
        }
        if(StringUtils.isEmpty(poemId)){
            throw new PingZeException("收藏的诗词id不能为空");
        }
        PoemUserCollect add=new PoemUserCollect();
        add.setId(UUID.randomUUID().toString());
        add.setAccount(account);
        add.setPoemId(poemId);
        repository.save(add);
        return add;
    }
    public PoemUserCollect edit(String id,String account,String poemId){
        PoemUserCollect updater= repository.findPoemUserCollectById(id);
        if(!updater.getAccount().equals(account)){
            updater.setAccount(account);
        }
        if(!updater.getPoemId().equals(poemId)){
            updater.setPoemId(poemId);
        }
        repository.save(updater);
        return  updater;
    }

    public PoemUserCollect findPoemUserCollectById(String id) {
        return repository.findPoemUserCollectById(id);
    }
}
